<?php
namespace App\Api;

use PhalApi\Api;
use App\Domain\Goods as DomainGoods;
use PhalApi\Exception\BadRequestException;

/**
 * 商品功能接口
 */
class Goods extends Api {
    public function getRules() {
        return array(
            'index' => array(
//                'is_new' => array('name' => 'is_new', 'require' => false, 'desc' => '新品:0非新品,1新品;默认为1'),
                'is_hot' => array('name' => 'is_hot', 'require' => false, 'desc' => '热销:0不热销,1热销;默认为0'),
                'goods_name' => array('name' => 'goods_name', 'require' => false, 'desc' => '搜索商品名'),
                'prom_type' => array('name' => 'prom_type', 'require' => false, 'desc' => '商品类型：0默认,3促销,6拼团;'),
                'is_recommend' => array('name' => 'is_recommend', 'require' => false, 'desc' => '是否推荐:0不推荐,1推荐;默认为0'),
                'cat_id' => array('name' => 'cat_id', 'require' => false, 'desc' => '商品分类:默认为全部分类（待解决）'),
                'curpage' => array('name' => 'curpage', 'require' => true, 'desc' => '当前页，默认为1'),
            ),
            'goodsDetail' => array(
                'goods_id' => array('name' => 'goods_id', 'require' => true, 'desc' => '商品id'),
            ),
            'addCart' => array(
                'user_id' => array('name' => 'user_id', 'require' => true, 'desc' => '用户id'),
                'goods_id' => array('name' => 'goods_id', 'require' => true, 'desc' => '商品id'),
                'goods_name' => array('name' => 'goods_name', 'require' => true, 'desc' => '商品名称'),
                'spec_key' => array('name' => 'spec_key', 'require' => true, 'desc' => '商品规格key'),
                'prom_type' => array('name' => 'prom_type', 'require' => true, 'desc' => '商品优惠类型：0默认3优惠促销6拼团'),
                'goods_price' => array('name' => 'goods_price', 'require' => true, 'desc' => '购买价'),
                'goods_num' => array('name' => 'goods_num', 'require' => true, 'desc' => '商品数量'),
            ),
            'cartList' => array(
                'user_id' => array('name' => 'user_id', 'require' => true, 'desc' => '用户id'),
            ),
            'cartDel' => array(
                'cart_id' => array('name' => 'cart_id', 'require' => true, 'desc' => '购物车id'),
            ),
            'ajaxCart' => array(
                'user_id' => array('name' => 'user_id', 'require' => true, 'desc' => '用户id'),
                'cart_id' => array('name' => 'cart_id', 'require' => true, 'desc' => '购物车商品id'),
                'goods_num' => array('name' => 'goods_num', 'require' => false, 'desc' => '要修改成为的数量'),
            ),
            'goodsComments' => array(
                'goods_id' => array('name' => 'goods_id', 'require' => true, 'desc' => '商品id'),
                'curpage' => array('name' => 'curpage', 'require' => true, 'desc' => '当前页，默认为1'),
            ),
        );
    }

    /**
     * 商品列表接口
     * @desc 获取商品列表
     * @return int goods_id 商品id
     * @return int cat_id 分类id
     * @return string goods_name 商品名称
     * @return string original_img 商品图片
     * @return string goods_remark 商品简要描述
     * @return decimal market_price 市场价
     * @return decimal shop_price 本店价
     * @return decimal prom_price 活动价格
     * @return int prom_id 活动id
     * @return int prom_type 商品类型：0默认,3促销,6拼团
     * @return string virtual_sales_sum 销量
     */
    public function index() {
        $condition = array();
        $str = 'add_time desc';
        $like = '';
        $condition['not store_count'] = array(0);
        $condition['is_on_sale'] = 1;
        if( isset( $this->is_hot ) ){
            $str = 'add_time,virtual_sales_sum desc';
        }
        if( isset( $this->goods_name ) ){
            $like = $this->goods_name;
        }
        if( isset( $this->prom_type ) ){
            $condition['prom_type'] = $this->prom_type;
            if( $condition['prom_type'] == 3 ){
                $condition['not store_count'] = array(0);
            }
            if( $condition['prom_type'] == 6 ){
                $condition['not store_count'] = array(0);
            }
        }else{
            $condition['prom_type'] = 0;
        }
        if( isset( $this->is_recommend ) ){
            $condition['is_recommend'] = $this->is_recommend;
        }
        if( !empty( $this->cat_id ) ){
            $condition['cat_id'] = $this->cat_id;
        }
        $domain = new DomainGoods();
        $data = $domain -> getGoods( $condition, $str, $like, $this->curpage );
        if( empty( $data ) ){
            throw new BadRequestException('数据查询失败', 1);
        }
        return $data;
    }

    /**
     * 商品详情接口
     * @desc 获取商品详情
     * @return int goods_id 商品id
     * @return int cat_id 分类id
     * @return string goods_name 商品名称
     * @return decimal market_price 市场价
     * @return decimal shop_price 本店价（促销时为促销价）
     * @return string virtual_sales_sum 销量
     * @return int prom_type 商品优惠类型：0默认3优惠促销6拼团
     * @return int prom_id 活动id
     * @return int prom_price 活动价格
     * @return string goods_content_img 图文详情
     * @return int comment_count 评论数
     * @return array images 头图
     * @return array comment 评论
     * @return array spec 规格
     */
    public function goodsDetail() {
        $domain = new DomainGoods();
        $data = $domain -> getGoodsDetail( $this->goods_id );
        if( empty( $data ) ){
            throw new BadRequestException('数据查询失败', 1);
        }
        return $data;
    }

    /**
     * 添加商品到购物车接口
     * @desc 添加商品到购物车
     */
    public function addCart() {
        $domain = new DomainGoods();
        $data = array();
        $data['user_id'] = $this->user_id;
        $data['goods_id'] = $this->goods_id;
        $data['goods_name'] = $this->goods_name;
        $data['spec_key'] = $this->spec_key;
        $data['prom_type'] = $this->prom_type;
        $data['goods_price'] = $this->goods_price;
        $data['goods_num'] = $this->goods_num;
        $data['add_time'] = time();
        $data = $domain -> addCart( $data );
        if( empty( $data ) ){
            throw new BadRequestException('数据添加失败', 1);
        }
        return $data;
    }

    /**
     * 购物车列表接口
     * @desc 购物车列表
     * @return int user_id 用户id
     * @return int goods_id 商品id
     * @return string goods_name 商品名称
     * @return string spec 所选规格
     * @return decimal market_price 市场价
     * @return decimal goods_price 购买价
     * @return string goods_num 购买数量
     * @return int prom_type 商品优惠类型：0默认3优惠促销6拼团
     * @return string images 商品展示图
     * @return string admin_id 分销商id
     */
    public function cartList() {
        $domain = new DomainGoods();
        $data = $domain -> cartList( $this->user_id );
        if( empty( $data ) ){
            throw new BadRequestException('数据获取失败', 1);
        }
        return $data;
    }

    /**
     * 购物车删除接口
     * @desc 购物车删除
     */
    public function cartDel() {
        $domain = new DomainGoods();
        $res = $domain -> cartDel( $this->cart_id );
        if( empty( $res ) ){
            throw new BadRequestException('数据删除失败', 1);
        }
        return $res;
    }

    /**
     * 购物车列表直接点击添加，减少接口
     * @desc 购物车列表直接点击添加，减少操作
     */
    public function ajaxCart() {
        $domain = new DomainGoods();
        $res = $domain -> ajaxCart( $this->user_id, $this->cart_id, $this->goods_num );
        if( empty( $res ) ){
            throw new BadRequestException('数据操作失败', 1);
        }
        return $res;
    }

    /**
     * 商品全部评论
     * @desc 商品全部评论
     *
     */
    public function goodsComments() {
        $domain = new DomainGoods();
        $res = $domain -> goodsComments( $this->goods_id, $this->curpage );
        if( empty($res['data']) ){
            $res = array();
        }
        return $res;
    }
}
